﻿using System.Collections.ObjectModel;
using System.Windows.Automation;
using ProdControls.Interfaces;
using ProdControls.UIAPatterns;
using ProdLogging;

namespace ProdControls.Bridges
{
    internal static class TableBridge
    {
        #region Required

        /// <summary>
        ///   Retrieves the primary direction of traversal ( ColumnMajor, RowMajor, Indeterminate) for the table
        /// </summary>
        /// <param name="extension"> The extended interface. </param>
        /// <param name="control"> The UI Automation element </param>
        /// <returns> The primary direction of traversal. </returns>
        public static RowOrColumnMajor RowOrColumnMajorHook(this ITable extension, BaseProdControl control)
        {
            return UiaGetRowOrColumnMajor(control);
        }

        private static RowOrColumnMajor UiaGetRowOrColumnMajor(BaseProdControl control)
        {
            RowOrColumnMajor retVal = TablePatternHelper.GetRowOrColumnMajor(control.UiaElement);
            LogController.ReceiveLogMessage(new LogMessage(retVal.ToString()));
            return retVal;
        }


        /// <summary>
        ///   Retrieves a collection of AutomationElements representing all the column headers in a table.
        /// </summary>
        /// <param name="extension"> The extension. </param>
        /// <param name="control"> The control. </param>
        /// <returns> </returns>
        public static AutomationElement[] ColumnHeadersHook(this ITable extension, BaseProdControl control)
        {
            return UiaGetColumnHeaders(control);
        }

        private static AutomationElement[] UiaGetColumnHeaders(BaseProdControl control)
        {
            AutomationElement[] retVal = TablePatternHelper.GetColumnHeaders(control.UiaElement);
            Collection<object> retList = new Collection<object>();
            foreach (AutomationElement item in retVal)
            {
                retList.Add(item);
            }

            LogController.ReceiveLogMessage(new LogMessage("Headers", retList));

            return retVal;
        }

        /// <summary>
        ///   Retrieves a collection of AutomationElements representing all the row headers in a table.
        /// </summary>
        /// <param name="extension"> The extension. </param>
        /// <param name="control"> The control. </param>
        /// <returns> </returns>
        public static AutomationElement[] RowHeadersHook(this ITable extension, BaseProdControl control)
        {
            return UiaGetRowHeaders(control);
        }

        private static AutomationElement[] UiaGetRowHeaders(BaseProdControl control)
        {
            AutomationElement[] retVal = TablePatternHelper.GetRowHeaders(control.UiaElement);
            Collection<object> retList = new Collection<object>(retVal);

            LogController.ReceiveLogMessage(new LogMessage("Headers", retList));

            return retVal;
        }

        /// <summary>
        ///   Retrieves an AutomationElement that represents the specified cell
        /// </summary>
        /// <param name="extension"> The extension. </param>
        /// <param name="control"> The control. </param>
        /// <param name="row"> The ordinal number of the row of interest. </param>
        /// <param name="column"> The ordinal number of the column of interest. </param>
        /// <returns> An AutomationElement that represents the retrieved cell </returns>
        public static AutomationElement GetItemHook(this ITable extension, BaseProdControl control, int row,
                                                    int column)
        {
            return UiaGetItem(control, row, column);
        }

        private static AutomationElement UiaGetItem(BaseProdControl control, int row, int column)
        {
            AutomationElement retVal = TablePatternHelper.GetItem(control.UiaElement, row, column);

            LogController.ReceiveLogMessage(new LogMessage("Item:" + retVal.Current.Name));
            return retVal;
        }

        #endregion

        /// <summary>
        ///   Gets the number of rows in the table.
        /// </summary>
        /// <param name="extension"> The extension. </param>
        /// <param name="control"> The control. </param>
        /// <returns> The number of rows in the current table </returns>
        public static int GetRowCountHook(this ITable extension, BaseProdControl control)
        {
            int retVal = TablePatternHelper.GetRowCount(control.UiaElement);

            LogController.ReceiveLogMessage(new LogMessage("Row Count:" + retVal));

            return retVal;
        }

        /// <summary>
        ///   Gets the number of columns in the table.
        /// </summary>
        /// <param name="extension"> The extension. </param>
        /// <param name="control"> The control. </param>
        /// <returns> The number of columns in the current table </returns>
        public static int GetColumnCountHook(this ITable extension, BaseProdControl control)
        {
            int retVal = TablePatternHelper.GetColumnCount(control.UiaElement);

            LogController.ReceiveLogMessage(new LogMessage("Column Count:" + retVal));

            return retVal;
        }
    }
}